/*
 * Copyright (C) 2018 Logos Technology Ltd.
 */

package com.logos.common.source.network.interceptor;

import com.logos.tool.JsonUtil;
import com.logos.tool.log.LogUtil;

/**
 * @author Young Ken
 */

public class LoggerFactory implements LoggerInterceptor.Logger {

    private LoggerFactory() {

    }

    private StringBuilder mMessage = new StringBuilder();

    @Override
    public void log(String message) {
        // 请求或者响应开始
        if (message.startsWith("--> POST")) {
            mMessage.setLength(0);
        }
        // 以{}或者[]形式的说明是响应结果的json数据，需要进行格式化
        if ((message.startsWith("{") && message.endsWith("}"))
                || (message.startsWith("[") && message.endsWith("]"))) {
            message = JsonUtil.formatJson(message);
        }
        mMessage.append(message.concat("\n"));
        // 请求或者响应结束，打印整条日志
        if (message.startsWith("<-- END HTTP")) {
            LogUtil.debug("LogHttpInfo", "\n" + mMessage.toString());
        }
    }

    /**
     * create factory
     *
     * @return LoggerFactory
     */
    public static LoggerFactory create() {
        return new LoggerFactory();
    }
}
